//
//  BYDetailViewController.swift
//  tableViewCell-Test
//
//  Created by 胡忠诚 on 15/12/13.
//  Copyright © 2015年 biyu6. All rights reserved.
//

import UIKit

class BYDetailViewController: UIViewController {

    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var ageTextField: UITextField!
    
    var person: BYPerson?//定义一个接收数据
    var saveCallBack:( () -> () )? //定义闭包,来进行数据的保存回调
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //赋值操作
        nameTextField.text = person?.name
        ageTextField.text = "\(person?.age ?? 0)"
    }
    //保存按钮
    @IBAction func save(sender: AnyObject) {
        //1.保存数据
        person?.name = nameTextField.text
        person?.age = Int(ageTextField.text!) ?? 0
        saveCallBack?()
        
        //2.返回页面
        navigationController?.popViewControllerAnimated(true)
    }
    //监听文本框的改变(设置保存按钮的状态)
    @IBAction func valueChange() {
        
        navigationItem.rightBarButtonItem?.enabled = nameTextField.hasText() && ageTextField.hasText()
    }
}


